package com.idudiao.sc.common.core.util;

import cn.hutool.core.util.StrUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils {

    private static final String DATE_STR = "yyyyMMdd HH:mm:ss";

    public static String format(LocalDateTime dateTime) {
        return format(dateTime, DATE_STR);
    }
    public static String format(LocalDateTime dateTime, String dateStr) {
        if (null == dateTime) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(StrUtil.isNotBlank(dateStr) ? dateStr : DATE_STR);
        return dateTime.format(formatter);
    }

    public static String getToday(String dateStr) {
        DateFormat sdf = new SimpleDateFormat(StrUtil.isNotBlank(dateStr) ? dateStr : DATE_STR);
        return sdf.format(new Date());
    }
    public static LocalDateTime parse(String dateTime) {
        return parse(dateTime, DATE_STR);
    }
    public static LocalDateTime parse(String dateTime, String dateStr) {
        if (StrUtil.isBlank(dateTime)){
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(StrUtil.isNotBlank(dateStr) ? dateStr : DATE_STR);
        return LocalDateTime.parse(dateTime, formatter);
    }


}
